{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 14, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>Vectors: Inner Product</h2>\n",
    "\n",
    "Two vectors can be multiplied with each other.\n",
    "\n",
    "But, <u>the result is a single number</u>, instead of a vector.\n",
    "\n",
    "Therefore, it is called <b>inner product</b> of two vectors, and so we can also avoid from any confusion with the ordinary multiplication.\n",
    "\n",
    "Consider the following two vectors:\n",
    "$$\n",
    "    u = \\myrvector{-3 \\\\ -2 \\\\ 0 \\\\ -1 \\\\ 4} \\mbox{ and } v = \\myrvector{-1\\\\ -1 \\\\2 \\\\ -3 \\\\ 5}.\n",
    "$$\n",
    "\n",
    "The inner product of $ u $ and $ v $, denoted $ \\inner{u}{v}$, can be defined algorithmically.\n",
    "\n",
    "<u>Pairwise multiplication</u>: the values in the same positions are multiplied with each other.\n",
    "\n",
    "<u>Summation of all pairwise multiplications</u>: Then we sum all the results obtained from the pairwise multiplications.\n",
    "\n",
    "Let's write its python code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's define both vectors\n",
    "u = [-3,-2,0,-1,4]\n",
    "v = [-1,-1,2,-3,5]\n",
    "\n",
    "uv = 0; # summation is initially zero\n",
    "\n",
    "for i in range(len(u)): # iteratively access every pair with the same indices\n",
    "    print(\"pairwise multiplication of the entries with index\",i,\"is\",u[i]*v[i])\n",
    "    uv = uv + u[i]*v[i] # i-th entries are multiplied and then added to summation\n",
    "    \n",
    "print() # print an empty line    \n",
    "print(\"The inner product of\",u,'and',v,'is',uv)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The pairwise multiplications of entries are\n",
    "<ul>\n",
    "    <li> $ (-3)\\cdot(-1) = 3 $, </li>\n",
    "    <li> $ (-2)\\cdot(-1) = 2 $, </li>\n",
    "    <li> $ 0\\cdot 2 = 0 $, </li>\n",
    "    <li> $ (-1)\\cdot(-3) = 3 $, and, </li>\n",
    "    <li> $ 4 \\cdot 5  = 20 $. </li>\n",
    "</ul>\n",
    "\n",
    "Thus the summation of all pairwise multiplication of entries is $ 3+2+0+3+20 = 28 $.\n",
    "\n",
    "<b>Remark that the dimensions of the given vectors must be the same. Otherwise, the inner product is not defined.</b>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 1 </h3>\n",
    "\n",
    "Find the inner product of the following vectors in python:\n",
    "\n",
    "$$\n",
    "    v = \\myrvector{-3 \\\\ 4 \\\\ -5 \\\\ 6} ~~~~\\mbox{and}~~~~ u = \\myrvector{4 \\\\ 3 \\\\ 6 \\\\ 5}.\n",
    "$$\n",
    "\n",
    "Your outcome should be $0$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B18_Python_Lists_Inner_Product_Solutions.ipynb#task1\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 2 </h3>\n",
    "\n",
    "Let $ u = \\myrvector{ -3 \\\\ -4 } $ be a 2 dimensional vector.\n",
    "\n",
    "Find $ \\inner{u}{u} $ in python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B18_Python_Lists_Inner_Product_Solutions.ipynb#task2\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b> Notes:</b>\n",
    "\n",
    "As you may have observed from the second task, the <b>length</b> of a vector can be calculated by using its <b>inner product</b> with itself.\n",
    "\n",
    "$$ \\norm{u} = \\sqrt{\\inner{u}{u}}. $$\n",
    "\n",
    "$ \\inner{u}{u} $ is $25$, and so $ \\norm{u} = \\sqrt{25} = 5 $. \n",
    "\n",
    "$ \\inner{u}{u} $ automatically accumulates the contribution of each entry to the length."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Orthogonal (perpendicular) vectors </h3>\n",
    "\n",
    "For simplicity, we consider 2-dimensional vectors.\n",
    "\n",
    "The following two vectors are perpendicular (orthogonal) to each other.\n",
    "\n",
    "The angle between them is $ 90 $ degrees. \n",
    "\n",
    "<img src=\"../images/vector_-4_-5-small.jpg\" width=\"40%\">\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's find the inner product of v and u\n",
    "v = [-4,0]\n",
    "u = [0,-5]\n",
    "result = 0;\n",
    "\n",
    "for i in range(2):\n",
    "    result = result + v[i]*u[i]\n",
    "\n",
    "print(\"the inner product of u and v is\",result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, let's check the inner product of the following two vectors:\n",
    "\n",
    "<img src=\"../images/length_v_u.jpg\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we can use the same code\n",
    "v = [-4,3]\n",
    "u = [-3,-4]\n",
    "result = 0;\n",
    "\n",
    "for i in range(2):\n",
    "    result = result + v[i]*u[i]\n",
    "\n",
    "print(\"the inner product of u and v is\",result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The inner product of new $ u $ and $ v $ is also $0$. \n",
    "\n",
    "This is not suprising, because the vectors $u$ and $v$ (in both cases) are orthogonal to each other.\n",
    "\n",
    "<b>Fact:</b> \n",
    "<ul>\n",
    "    <li>The inner product of two orthogonal (perpendicular) vectors is zero.</li>\n",
    "    <li>If the inner product of two vectors is zero, then they are ortohogonal to each other.</li>\n",
    "</ul>\n",
    "\n",
    "<i> This fact is important, because, as we will see later, orthogonal vectors (states) are distingiushed perfectly. </i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 3 </h3>\n",
    "\n",
    "Verify that (i) $ u $ is orthogonal to $ -v $, (ii) $ -u $ is orthogonal to $ v $, and (iii) $ -u $ is orthogonal to $ -v $.\n",
    "\n",
    "<img src=\"../images/inner_v_u_-v_-u.jpg\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# you may consider to write a function in python for innner product\n",
    "\n",
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B18_Python_Lists_Inner_Product_Solutions.ipynb#task3\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 4 </h3>\n",
    "\n",
    "Find the inner product of $ u $ and $ v $ in python.\n",
    "\n",
    "$$\n",
    "    v = \\myrvector{-1 \\\\ 2 \\\\ -3 \\\\ 4} ~~~~\\mbox{and}~~~~ u = \\myrvector{-2 \\\\ -1 \\\\ 5 \\\\ 2}.\n",
    "$$\n",
    "\n",
    "Find the inner product of $ -2u $ and $ 3v $ in python.\n",
    "\n",
    "Compare both results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B18_Python_Lists_Inner_Product_Solutions.ipynb#task4\">click for our solution</a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
